package com.ls.controller.customer.model;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ls.entity.Customer;
import com.ls.enums.SignEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static com.ls.constant.SystemConstants.IS_PLATFORM_SIGN_FALSE;
import static com.ls.enums.OrderRecordEnum.getLowerScore;
import static com.ls.constant.SystemConstants.PLATFORM_SIGN_CONTENT;

@Slf4j
@Data
public class GetUserInfoModel {
    @ApiModelProperty(value = "是否会员 0否，1会员，-1过期")
    private Integer memberStatus;
    @ApiModelProperty(value = "会员等级")
    private Integer memberLevel;
    @ApiModelProperty(value = "发送通道，1 本机 2 平台")
    private Integer channelType;
    @ApiModelProperty(value = "性别 1：男 2：女 3：未知")
    public Integer sex;
    @ApiModelProperty(value = "积分")
    private Integer memberScore;
    @ApiModelProperty(value = "会员短信剩余条数")
    private Integer messageNum;
    @ApiModelProperty(value = "会员短信当月总条数")
    private Integer messageNumTotal;
    @ApiModelProperty(value = "短信套餐剩余条数")
    private Integer smsThaliNum;
    @ApiModelProperty(value = "短信套餐当月总条数")
    private Integer smsThaliNumTotal;
    @ApiModelProperty(value = "本月可发数量")
    private Integer currentQuantity;
    @ApiModelProperty(value = "本月可发短信剩余总条数")
    private Integer smsTotal;
    @ApiModelProperty(value = "本月已发平台短信数量")
    private Integer currentSendTimes;
    @ApiModelProperty(value = "本手机发送总数")
    private Integer phoneSendTimes;
    @ApiModelProperty(value = "平台发送短信总数")
    private Integer platformSendTimes;
    @ApiModelProperty(value = "发送短信次数")
    private Integer sendTimes;
    @ApiModelProperty(value = "消费总金额")
    private Integer consumeAmount;
    @ApiModelProperty(value = "用户ID")
    private Long customerId;
    @ApiModelProperty(value = "生日")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JSONField(format = "yyyy-MM-dd")
    public LocalDate birthday;
    @ApiModelProperty(value = "生日类型")
    @Max(value = 1)
    @Min(value = 0)
    private Integer birthdayType;
    @ApiModelProperty(value = "手机号")
    private String phone;
    @ApiModelProperty(value = "用户头像")
    private String avatar;
    @ApiModelProperty(value = "签名")
    private String sign;
    @ApiModelProperty(value = "签名待审核")
    private String signVerify;
    @ApiModelProperty(value = "用户昵称")
    private String nickname;
    @ApiModelProperty(value = "会员过期时间")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JSONField(format = "yyyy-MM-dd")
    private LocalDate memberExpireTime;
    @ApiModelProperty(value = "当月会员短信清除时间")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JSONField(format = "yyyy-MM-dd")
    private LocalDate memberClearTime;
    @ApiModelProperty(value = "短信套餐过期时间")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JSONField(format = "yyyy-MM-dd")
    private LocalDate smsExpireTime;
    @ApiModelProperty(value = "当期短信套餐清除时间")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JSONField(format = "yyyy-MM-dd")
    private LocalDate smsClearTime;
    /**
     * 是否使用平台签名，0使用自定义，1使用乐信通，默认乐信通，2使用智惠信使
     */
    @ApiModelProperty(value = "是否使用平台签名")
    private Integer isPlatformSign;
    @ApiModelProperty(value = "优惠券张数")
    private Integer couponCount;
    @ApiModelProperty(value = "距离下级积分")
    private Integer lowerLevelPoints;
    // 新增全参构造方法
    public GetUserInfoModel(Customer customer) {
        log.info("获取用户信息={}", customer);
        this.customerId = customer.getCustomerId();
        this.nickname = customer.getNickname();
        this.phone = customer.getPhone();
        this.birthday = customer.getBirthday();
        this.birthdayType = customer.getBirthdayType();
        this.sex = customer.getSex();
        //是否是会员，默认  0 :否  -1:过期  1:是会员
        this.memberStatus = customer.getMemberStatus();
        //会员等级
        log.info("会员等级={}", customer.getMemberLevel());
        this.memberLevel = customer.getMemberLevel();
        //发送通道
        this.channelType = customer.getChannelType();
        //是否使用平台签名，0使用自定义，1使用乐信通，默认乐信通，2使用智惠信使
        this.isPlatformSign = customer.getIsPlatformSign();
        if(customer.getIsPlatformSign()!=IS_PLATFORM_SIGN_FALSE){
            this.sign = SignEnum.getSign(customer.getIsPlatformSign());
        }else{
            this.sign = customer.getSign();
        }
        //会员积分
        this.memberScore = customer.getMemberScore();
        //短信剩余条数
        this.messageNum = customer.getMessageNum();
        this.messageNumTotal = customer.getMessageNumTotal();
        //短信套餐剩余条数
        this.smsThaliNum = customer.getSmsThaliNum();
        this.smsThaliNumTotal = customer.getSmsThaliNumTotal();
        this.smsTotal = customer.getSmsTotal();
        this.consumeAmount = customer.getConsumeAmount().intValue();
        this.currentQuantity = customer.getCurrentQuantity();
        this.currentSendTimes = customer.getCurrentSendTimes();
        this.phoneSendTimes = customer.getPhoneSendTimes();
        this.platformSendTimes = customer.getPlatformSendTimes();
        this.sendTimes = customer.getSendTimes();
        this.avatar = customer.getAvatar();
        //会员过期时间
        this.memberExpireTime= convertToLocalDate(customer.getMemberExpireTime());
        this.memberClearTime = convertToLocalDate(customer.getMemberClearTime());
        this.smsExpireTime = convertToLocalDate(customer.getSmsExpireTime());
        this.smsClearTime = convertToLocalDate(customer.getSmsClearTime());
        this.couponCount = 0;
        this.lowerLevelPoints = getLowerScore(customer.getMemberScore());
        this.signVerify = customer.getSignVerify();
    }
    private LocalDate convertToLocalDate(LocalDateTime dateTime) {
        return dateTime != null ? dateTime.toLocalDate() : null;
    }
}
